<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>tetrixboard.cpp 示例文件 | Qt Widgets 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtwidgets-index.html">Qt Widgets</a></td><td ><a href="qtwidgets-widgets-tetrix-example.html">Tetrix Example</a></td><td >tetrixboard.cpp 示例文件</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtwidgets-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar"><div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">tetrixboard.cpp 示例文件</h1>
<span class="subtitle">widgets/tetrix/tetrixboard.cpp</span>
<!-- $$$widgets/tetrix/tetrixboard.cpp-description -->
<div class="descr"> <a name="details"></a>
<pre class="cpp">

  <span class="comment">/****************************************************************************
  **
  ** Copyright (C) 2016 The Qt Company Ltd.
  ** Contact: https://www.qt.io/licensing/
  **
  ** This file is part of the examples of the Qt Toolkit.
  **
  ** $QT_BEGIN_LICENSE:BSD$
  ** Commercial License Usage
  ** Licensees holding valid commercial Qt licenses may use this file in
  ** accordance with the commercial license agreement provided with the
  ** Software or, alternatively, in accordance with the terms contained in
  ** a written agreement between you and The Qt Company. For licensing terms
  ** and conditions see https://www.qt.io/terms-conditions. For further
  ** information use the contact form at https://www.qt.io/contact-us.
  **
  ** BSD License Usage
  ** Alternatively, you may use this file under the terms of the BSD license
  ** as follows:
  **
  ** &quot;Redistribution and use in source and binary forms, with or without
  ** modification, are permitted provided that the following conditions are
  ** met:
  **   * Redistributions of source code must retain the above copyright
  **     notice, this list of conditions and the following disclaimer.
  **   * Redistributions in binary form must reproduce the above copyright
  **     notice, this list of conditions and the following disclaimer in
  **     the documentation and/or other materials provided with the
  **     distribution.
  **   * Neither the name of The Qt Company Ltd nor the names of its
  **     contributors may be used to endorse or promote products derived
  **     from this software without specific prior written permission.
  **
  **
  ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  ** &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&quot;
  **
  ** $QT_END_LICENSE$
  **
  ****************************************************************************/</span>

  <span class="preprocessor">#include &lt;QtWidgets&gt;</span>

  <span class="preprocessor">#include &quot;tetrixboard.h&quot;</span>

  TetrixBoard<span class="operator">::</span>TetrixBoard(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent)
      : <span class="type"><a href="qframe.html">QFrame</a></span>(parent)
  {
      setFrameStyle(<span class="type"><a href="qframe.html">QFrame</a></span><span class="operator">::</span>Panel <span class="operator">|</span> <span class="type"><a href="qframe.html">QFrame</a></span><span class="operator">::</span>Sunken);
      setFocusPolicy(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>StrongFocus);
      isStarted <span class="operator">=</span> <span class="keyword">false</span>;
      isPaused <span class="operator">=</span> <span class="keyword">false</span>;
      clearBoard();

      nextPiece<span class="operator">.</span>setRandomShape();
  }

  <span class="type">void</span> TetrixBoard<span class="operator">::</span>setNextPieceLabel(<span class="type"><a href="qlabel.html">QLabel</a></span> <span class="operator">*</span>label)
  {
      nextPieceLabel <span class="operator">=</span> label;
  }

  <span class="type"><a href="../qtcore/qsize.html">QSize</a></span> TetrixBoard<span class="operator">::</span>sizeHint() <span class="keyword">const</span>
  {
      <span class="keyword">return</span> <span class="type"><a href="../qtcore/qsize.html">QSize</a></span>(BoardWidth <span class="operator">*</span> <span class="number">15</span> <span class="operator">+</span> frameWidth() <span class="operator">*</span> <span class="number">2</span><span class="operator">,</span>
                   BoardHeight <span class="operator">*</span> <span class="number">15</span> <span class="operator">+</span> frameWidth() <span class="operator">*</span> <span class="number">2</span>);
  }

  <span class="type"><a href="../qtcore/qsize.html">QSize</a></span> TetrixBoard<span class="operator">::</span>minimumSizeHint() <span class="keyword">const</span>
  {
      <span class="keyword">return</span> <span class="type"><a href="../qtcore/qsize.html">QSize</a></span>(BoardWidth <span class="operator">*</span> <span class="number">5</span> <span class="operator">+</span> frameWidth() <span class="operator">*</span> <span class="number">2</span><span class="operator">,</span>
                   BoardHeight <span class="operator">*</span> <span class="number">5</span> <span class="operator">+</span> frameWidth() <span class="operator">*</span> <span class="number">2</span>);
  }

  <span class="type">void</span> TetrixBoard<span class="operator">::</span>start()
  {
      <span class="keyword">if</span> (isPaused)
          <span class="keyword">return</span>;

      isStarted <span class="operator">=</span> <span class="keyword">true</span>;
      isWaitingAfterLine <span class="operator">=</span> <span class="keyword">false</span>;
      numLinesRemoved <span class="operator">=</span> <span class="number">0</span>;
      numPiecesDropped <span class="operator">=</span> <span class="number">0</span>;
      score <span class="operator">=</span> <span class="number">0</span>;
      level <span class="operator">=</span> <span class="number">1</span>;
      clearBoard();

      <span class="keyword">emit</span> linesRemovedChanged(numLinesRemoved);
      <span class="keyword">emit</span> scoreChanged(score);
      <span class="keyword">emit</span> levelChanged(level);

      newPiece();
      timer<span class="operator">.</span>start(timeoutTime()<span class="operator">,</span> <span class="keyword">this</span>);
  }

  <span class="type">void</span> TetrixBoard<span class="operator">::</span>pause()
  {
      <span class="keyword">if</span> (<span class="operator">!</span>isStarted)
          <span class="keyword">return</span>;

      isPaused <span class="operator">=</span> <span class="operator">!</span>isPaused;
      <span class="keyword">if</span> (isPaused) {
          timer<span class="operator">.</span>stop();
      } <span class="keyword">else</span> {
          timer<span class="operator">.</span>start(timeoutTime()<span class="operator">,</span> <span class="keyword">this</span>);
      }
      update();
  }

  <span class="type">void</span> TetrixBoard<span class="operator">::</span>paintEvent(<span class="type"><a href="../qtgui/qpaintevent.html">QPaintEvent</a></span> <span class="operator">*</span>event)
  {
      <span class="type"><a href="qframe.html">QFrame</a></span><span class="operator">::</span>paintEvent(event);

      <span class="type"><a href="../qtgui/qpainter.html">QPainter</a></span> painter(<span class="keyword">this</span>);
      <span class="type"><a href="../qtcore/qrect.html">QRect</a></span> rect <span class="operator">=</span> contentsRect();

      <span class="keyword">if</span> (isPaused) {
          painter<span class="operator">.</span>drawText(rect<span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>AlignCenter<span class="operator">,</span> tr(<span class="string">&quot;Pause&quot;</span>));
          <span class="keyword">return</span>;
      }

      <span class="type">int</span> boardTop <span class="operator">=</span> rect<span class="operator">.</span>bottom() <span class="operator">-</span> BoardHeight<span class="operator">*</span>squareHeight();

      <span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span>; i <span class="operator">&lt;</span> BoardHeight; <span class="operator">+</span><span class="operator">+</span>i) {
          <span class="keyword">for</span> (<span class="type">int</span> j <span class="operator">=</span> <span class="number">0</span>; j <span class="operator">&lt;</span> BoardWidth; <span class="operator">+</span><span class="operator">+</span>j) {
              TetrixShape shape <span class="operator">=</span> shapeAt(j<span class="operator">,</span> BoardHeight <span class="operator">-</span> i <span class="operator">-</span> <span class="number">1</span>);
              <span class="keyword">if</span> (shape <span class="operator">!</span><span class="operator">=</span> NoShape)
                  drawSquare(painter<span class="operator">,</span> rect<span class="operator">.</span>left() <span class="operator">+</span> j <span class="operator">*</span> squareWidth()<span class="operator">,</span>
                             boardTop <span class="operator">+</span> i <span class="operator">*</span> squareHeight()<span class="operator">,</span> shape);
          }
      }

      <span class="keyword">if</span> (curPiece<span class="operator">.</span>shape() <span class="operator">!</span><span class="operator">=</span> NoShape) {
          <span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span>; i <span class="operator">&lt;</span> <span class="number">4</span>; <span class="operator">+</span><span class="operator">+</span>i) {
              <span class="type">int</span> x <span class="operator">=</span> curX <span class="operator">+</span> curPiece<span class="operator">.</span>x(i);
              <span class="type">int</span> y <span class="operator">=</span> curY <span class="operator">-</span> curPiece<span class="operator">.</span>y(i);
              drawSquare(painter<span class="operator">,</span> rect<span class="operator">.</span>left() <span class="operator">+</span> x <span class="operator">*</span> squareWidth()<span class="operator">,</span>
                         boardTop <span class="operator">+</span> (BoardHeight <span class="operator">-</span> y <span class="operator">-</span> <span class="number">1</span>) <span class="operator">*</span> squareHeight()<span class="operator">,</span>
                         curPiece<span class="operator">.</span>shape());
          }
      }
  }

  <span class="type">void</span> TetrixBoard<span class="operator">::</span>keyPressEvent(<span class="type"><a href="../qtgui/qkeyevent.html">QKeyEvent</a></span> <span class="operator">*</span>event)
  {
      <span class="keyword">if</span> (<span class="operator">!</span>isStarted <span class="operator">|</span><span class="operator">|</span> isPaused <span class="operator">|</span><span class="operator">|</span> curPiece<span class="operator">.</span>shape() <span class="operator">=</span><span class="operator">=</span> NoShape) {
          <span class="type"><a href="qframe.html">QFrame</a></span><span class="operator">::</span>keyPressEvent(event);
          <span class="keyword">return</span>;
      }

      <span class="keyword">switch</span> (event<span class="operator">-</span><span class="operator">&gt;</span>key()) {
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>Key_Left:
          tryMove(curPiece<span class="operator">,</span> curX <span class="operator">-</span> <span class="number">1</span><span class="operator">,</span> curY);
          <span class="keyword">break</span>;
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>Key_Right:
          tryMove(curPiece<span class="operator">,</span> curX <span class="operator">+</span> <span class="number">1</span><span class="operator">,</span> curY);
          <span class="keyword">break</span>;
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>Key_Down:
          tryMove(curPiece<span class="operator">.</span>rotatedRight()<span class="operator">,</span> curX<span class="operator">,</span> curY);
          <span class="keyword">break</span>;
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>Key_Up:
          tryMove(curPiece<span class="operator">.</span>rotatedLeft()<span class="operator">,</span> curX<span class="operator">,</span> curY);
          <span class="keyword">break</span>;
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>Key_Space:
          dropDown();
          <span class="keyword">break</span>;
      <span class="keyword">case</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>Key_D:
          oneLineDown();
          <span class="keyword">break</span>;
      <span class="keyword">default</span>:
          <span class="type"><a href="qframe.html">QFrame</a></span><span class="operator">::</span>keyPressEvent(event);
      }
  }

  <span class="type">void</span> TetrixBoard<span class="operator">::</span>timerEvent(<span class="type"><a href="../qtcore/qtimerevent.html">QTimerEvent</a></span> <span class="operator">*</span>event)
  {
      <span class="keyword">if</span> (event<span class="operator">-</span><span class="operator">&gt;</span>timerId() <span class="operator">=</span><span class="operator">=</span> timer<span class="operator">.</span>timerId()) {
          <span class="keyword">if</span> (isWaitingAfterLine) {
              isWaitingAfterLine <span class="operator">=</span> <span class="keyword">false</span>;
              newPiece();
              timer<span class="operator">.</span>start(timeoutTime()<span class="operator">,</span> <span class="keyword">this</span>);
          } <span class="keyword">else</span> {
              oneLineDown();
          }
      } <span class="keyword">else</span> {
          <span class="type"><a href="qframe.html">QFrame</a></span><span class="operator">::</span>timerEvent(event);
      }
  }

  <span class="type">void</span> TetrixBoard<span class="operator">::</span>clearBoard()
  {
      <span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span>; i <span class="operator">&lt;</span> BoardHeight <span class="operator">*</span> BoardWidth; <span class="operator">+</span><span class="operator">+</span>i)
          board<span class="operator">[</span>i<span class="operator">]</span> <span class="operator">=</span> NoShape;
  }

  <span class="type">void</span> TetrixBoard<span class="operator">::</span>dropDown()
  {
      <span class="type">int</span> dropHeight <span class="operator">=</span> <span class="number">0</span>;
      <span class="type">int</span> newY <span class="operator">=</span> curY;
      <span class="keyword">while</span> (newY <span class="operator">&gt;</span> <span class="number">0</span>) {
          <span class="keyword">if</span> (<span class="operator">!</span>tryMove(curPiece<span class="operator">,</span> curX<span class="operator">,</span> newY <span class="operator">-</span> <span class="number">1</span>))
              <span class="keyword">break</span>;
          <span class="operator">-</span><span class="operator">-</span>newY;
          <span class="operator">+</span><span class="operator">+</span>dropHeight;
      }
      pieceDropped(dropHeight);
  }

  <span class="type">void</span> TetrixBoard<span class="operator">::</span>oneLineDown()
  {
      <span class="keyword">if</span> (<span class="operator">!</span>tryMove(curPiece<span class="operator">,</span> curX<span class="operator">,</span> curY <span class="operator">-</span> <span class="number">1</span>))
          pieceDropped(<span class="number">0</span>);
  }

  <span class="type">void</span> TetrixBoard<span class="operator">::</span>pieceDropped(<span class="type">int</span> dropHeight)
  {
      <span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span>; i <span class="operator">&lt;</span> <span class="number">4</span>; <span class="operator">+</span><span class="operator">+</span>i) {
          <span class="type">int</span> x <span class="operator">=</span> curX <span class="operator">+</span> curPiece<span class="operator">.</span>x(i);
          <span class="type">int</span> y <span class="operator">=</span> curY <span class="operator">-</span> curPiece<span class="operator">.</span>y(i);
          shapeAt(x<span class="operator">,</span> y) <span class="operator">=</span> curPiece<span class="operator">.</span>shape();
      }

      <span class="operator">+</span><span class="operator">+</span>numPiecesDropped;
      <span class="keyword">if</span> (numPiecesDropped <span class="operator">%</span> <span class="number">25</span> <span class="operator">=</span><span class="operator">=</span> <span class="number">0</span>) {
          <span class="operator">+</span><span class="operator">+</span>level;
          timer<span class="operator">.</span>start(timeoutTime()<span class="operator">,</span> <span class="keyword">this</span>);
          <span class="keyword">emit</span> levelChanged(level);
      }

      score <span class="operator">+</span><span class="operator">=</span> dropHeight <span class="operator">+</span> <span class="number">7</span>;
      <span class="keyword">emit</span> scoreChanged(score);
      removeFullLines();

      <span class="keyword">if</span> (<span class="operator">!</span>isWaitingAfterLine)
          newPiece();
  }

  <span class="type">void</span> TetrixBoard<span class="operator">::</span>removeFullLines()
  {
      <span class="type">int</span> numFullLines <span class="operator">=</span> <span class="number">0</span>;

      <span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> BoardHeight <span class="operator">-</span> <span class="number">1</span>; i <span class="operator">&gt;</span><span class="operator">=</span> <span class="number">0</span>; <span class="operator">-</span><span class="operator">-</span>i) {
          bool lineIsFull <span class="operator">=</span> <span class="keyword">true</span>;

          <span class="keyword">for</span> (<span class="type">int</span> j <span class="operator">=</span> <span class="number">0</span>; j <span class="operator">&lt;</span> BoardWidth; <span class="operator">+</span><span class="operator">+</span>j) {
              <span class="keyword">if</span> (shapeAt(j<span class="operator">,</span> i) <span class="operator">=</span><span class="operator">=</span> NoShape) {
                  lineIsFull <span class="operator">=</span> <span class="keyword">false</span>;
                  <span class="keyword">break</span>;
              }
          }

          <span class="keyword">if</span> (lineIsFull) {
              <span class="operator">+</span><span class="operator">+</span>numFullLines;
              <span class="keyword">for</span> (<span class="type">int</span> k <span class="operator">=</span> i; k <span class="operator">&lt;</span> BoardHeight <span class="operator">-</span> <span class="number">1</span>; <span class="operator">+</span><span class="operator">+</span>k) {
                  <span class="keyword">for</span> (<span class="type">int</span> j <span class="operator">=</span> <span class="number">0</span>; j <span class="operator">&lt;</span> BoardWidth; <span class="operator">+</span><span class="operator">+</span>j)
                      shapeAt(j<span class="operator">,</span> k) <span class="operator">=</span> shapeAt(j<span class="operator">,</span> k <span class="operator">+</span> <span class="number">1</span>);
              }
              <span class="keyword">for</span> (<span class="type">int</span> j <span class="operator">=</span> <span class="number">0</span>; j <span class="operator">&lt;</span> BoardWidth; <span class="operator">+</span><span class="operator">+</span>j)
                  shapeAt(j<span class="operator">,</span> BoardHeight <span class="operator">-</span> <span class="number">1</span>) <span class="operator">=</span> NoShape;
          }
      }

      <span class="keyword">if</span> (numFullLines <span class="operator">&gt;</span> <span class="number">0</span>) {
          numLinesRemoved <span class="operator">+</span><span class="operator">=</span> numFullLines;
          score <span class="operator">+</span><span class="operator">=</span> <span class="number">10</span> <span class="operator">*</span> numFullLines;
          <span class="keyword">emit</span> linesRemovedChanged(numLinesRemoved);
          <span class="keyword">emit</span> scoreChanged(score);

          timer<span class="operator">.</span>start(<span class="number">500</span><span class="operator">,</span> <span class="keyword">this</span>);
          isWaitingAfterLine <span class="operator">=</span> <span class="keyword">true</span>;
          curPiece<span class="operator">.</span>setShape(NoShape);
          update();
      }
  }

  <span class="type">void</span> TetrixBoard<span class="operator">::</span>newPiece()
  {
      curPiece <span class="operator">=</span> nextPiece;
      nextPiece<span class="operator">.</span>setRandomShape();
      showNextPiece();
      curX <span class="operator">=</span> BoardWidth <span class="operator">/</span> <span class="number">2</span> <span class="operator">+</span> <span class="number">1</span>;
      curY <span class="operator">=</span> BoardHeight <span class="operator">-</span> <span class="number">1</span> <span class="operator">+</span> curPiece<span class="operator">.</span>minY();

      <span class="keyword">if</span> (<span class="operator">!</span>tryMove(curPiece<span class="operator">,</span> curX<span class="operator">,</span> curY)) {
          curPiece<span class="operator">.</span>setShape(NoShape);
          timer<span class="operator">.</span>stop();
          isStarted <span class="operator">=</span> <span class="keyword">false</span>;
      }
  }

  <span class="type">void</span> TetrixBoard<span class="operator">::</span>showNextPiece()
  {
      <span class="keyword">if</span> (<span class="operator">!</span>nextPieceLabel)
          <span class="keyword">return</span>;

      <span class="type">int</span> dx <span class="operator">=</span> nextPiece<span class="operator">.</span>maxX() <span class="operator">-</span> nextPiece<span class="operator">.</span>minX() <span class="operator">+</span> <span class="number">1</span>;
      <span class="type">int</span> dy <span class="operator">=</span> nextPiece<span class="operator">.</span>maxY() <span class="operator">-</span> nextPiece<span class="operator">.</span>minY() <span class="operator">+</span> <span class="number">1</span>;

      <span class="type"><a href="../qtgui/qpixmap.html">QPixmap</a></span> pixmap(dx <span class="operator">*</span> squareWidth()<span class="operator">,</span> dy <span class="operator">*</span> squareHeight());
      <span class="type"><a href="../qtgui/qpainter.html">QPainter</a></span> painter(<span class="operator">&amp;</span>pixmap);
      painter<span class="operator">.</span>fillRect(pixmap<span class="operator">.</span>rect()<span class="operator">,</span> nextPieceLabel<span class="operator">-</span><span class="operator">&gt;</span>palette()<span class="operator">.</span>background());

      <span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span>; i <span class="operator">&lt;</span> <span class="number">4</span>; <span class="operator">+</span><span class="operator">+</span>i) {
          <span class="type">int</span> x <span class="operator">=</span> nextPiece<span class="operator">.</span>x(i) <span class="operator">-</span> nextPiece<span class="operator">.</span>minX();
          <span class="type">int</span> y <span class="operator">=</span> nextPiece<span class="operator">.</span>y(i) <span class="operator">-</span> nextPiece<span class="operator">.</span>minY();
          drawSquare(painter<span class="operator">,</span> x <span class="operator">*</span> squareWidth()<span class="operator">,</span> y <span class="operator">*</span> squareHeight()<span class="operator">,</span>
                     nextPiece<span class="operator">.</span>shape());
      }
      nextPieceLabel<span class="operator">-</span><span class="operator">&gt;</span>setPixmap(pixmap);
  }

  bool TetrixBoard<span class="operator">::</span>tryMove(<span class="keyword">const</span> TetrixPiece <span class="operator">&amp;</span>newPiece<span class="operator">,</span> <span class="type">int</span> newX<span class="operator">,</span> <span class="type">int</span> newY)
  {
      <span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span>; i <span class="operator">&lt;</span> <span class="number">4</span>; <span class="operator">+</span><span class="operator">+</span>i) {
          <span class="type">int</span> x <span class="operator">=</span> newX <span class="operator">+</span> newPiece<span class="operator">.</span>x(i);
          <span class="type">int</span> y <span class="operator">=</span> newY <span class="operator">-</span> newPiece<span class="operator">.</span>y(i);
          <span class="keyword">if</span> (x <span class="operator">&lt;</span> <span class="number">0</span> <span class="operator">|</span><span class="operator">|</span> x <span class="operator">&gt;</span><span class="operator">=</span> BoardWidth <span class="operator">|</span><span class="operator">|</span> y <span class="operator">&lt;</span> <span class="number">0</span> <span class="operator">|</span><span class="operator">|</span> y <span class="operator">&gt;</span><span class="operator">=</span> BoardHeight)
              <span class="keyword">return</span> <span class="keyword">false</span>;
          <span class="keyword">if</span> (shapeAt(x<span class="operator">,</span> y) <span class="operator">!</span><span class="operator">=</span> NoShape)
              <span class="keyword">return</span> <span class="keyword">false</span>;
      }

      curPiece <span class="operator">=</span> newPiece;
      curX <span class="operator">=</span> newX;
      curY <span class="operator">=</span> newY;
      update();
      <span class="keyword">return</span> <span class="keyword">true</span>;
  }

  <span class="type">void</span> TetrixBoard<span class="operator">::</span>drawSquare(<span class="type"><a href="../qtgui/qpainter.html">QPainter</a></span> <span class="operator">&amp;</span>painter<span class="operator">,</span> <span class="type">int</span> x<span class="operator">,</span> <span class="type">int</span> y<span class="operator">,</span> TetrixShape shape)
  {
      <span class="keyword">static</span> <span class="keyword">const</span> <span class="type"><a href="../qtgui/qcolor.html#QRgb-typedef">QRgb</a></span> colorTable<span class="operator">[</span><span class="number">8</span><span class="operator">]</span> <span class="operator">=</span> {
          <span class="number">0x000000</span><span class="operator">,</span> <span class="number">0xCC6666</span><span class="operator">,</span> <span class="number">0x66CC66</span><span class="operator">,</span> <span class="number">0x6666CC</span><span class="operator">,</span>
          <span class="number">0xCCCC66</span><span class="operator">,</span> <span class="number">0xCC66CC</span><span class="operator">,</span> <span class="number">0x66CCCC</span><span class="operator">,</span> <span class="number">0xDAAA00</span>
      };

      <span class="type"><a href="../qtgui/qcolor.html">QColor</a></span> color <span class="operator">=</span> colorTable<span class="operator">[</span><span class="type">int</span>(shape)<span class="operator">]</span>;
      painter<span class="operator">.</span>fillRect(x <span class="operator">+</span> <span class="number">1</span><span class="operator">,</span> y <span class="operator">+</span> <span class="number">1</span><span class="operator">,</span> squareWidth() <span class="operator">-</span> <span class="number">2</span><span class="operator">,</span> squareHeight() <span class="operator">-</span> <span class="number">2</span><span class="operator">,</span>
                       color);

      painter<span class="operator">.</span>setPen(color<span class="operator">.</span>light());
      painter<span class="operator">.</span>drawLine(x<span class="operator">,</span> y <span class="operator">+</span> squareHeight() <span class="operator">-</span> <span class="number">1</span><span class="operator">,</span> x<span class="operator">,</span> y);
      painter<span class="operator">.</span>drawLine(x<span class="operator">,</span> y<span class="operator">,</span> x <span class="operator">+</span> squareWidth() <span class="operator">-</span> <span class="number">1</span><span class="operator">,</span> y);

      painter<span class="operator">.</span>setPen(color<span class="operator">.</span>dark());
      painter<span class="operator">.</span>drawLine(x <span class="operator">+</span> <span class="number">1</span><span class="operator">,</span> y <span class="operator">+</span> squareHeight() <span class="operator">-</span> <span class="number">1</span><span class="operator">,</span>
                       x <span class="operator">+</span> squareWidth() <span class="operator">-</span> <span class="number">1</span><span class="operator">,</span> y <span class="operator">+</span> squareHeight() <span class="operator">-</span> <span class="number">1</span>);
      painter<span class="operator">.</span>drawLine(x <span class="operator">+</span> squareWidth() <span class="operator">-</span> <span class="number">1</span><span class="operator">,</span> y <span class="operator">+</span> squareHeight() <span class="operator">-</span> <span class="number">1</span><span class="operator">,</span>
                       x <span class="operator">+</span> squareWidth() <span class="operator">-</span> <span class="number">1</span><span class="operator">,</span> y <span class="operator">+</span> <span class="number">1</span>);
  }

</pre>
</div>
<!-- @@@widgets/tetrix/tetrixboard.cpp -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
